package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.SellerWallet;
import cn.signalpha.modules.shop.excel.SellerWalletExcel;
import cn.signalpha.modules.shop.mapper.SellerWalletMapper;
import cn.signalpha.modules.shop.vo.SellerWalletVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class SellerWalletService extends ServiceImpl<SellerWalletMapper, SellerWallet> {

	/**
	 * 自定义分页
	 */
    public IPage<SellerWalletVO> selectSellerWalletPage(IPage<SellerWalletVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectSellerWalletPage(page, params));
    }

    /**
	 * 自定义导出
	 */
    public List<SellerWalletExcel> selectSellerWalletExcel(Map<String, Object> params) {
        return BeanUtil.copy(baseMapper.selectSellerWalletPage(null, params), SellerWalletExcel.class);
    }

    /**
	 * 自定义Wrappers
	 */
    public QueryWrapper<SellerWallet> selectSellerWalletWrapper(Map<String, Object> params) {
        QueryWrapper<SellerWallet> wrapper = Wrappers.query();
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("sellerId")), SellerWallet::getSellerId, params.get("sellerId"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("type")), SellerWallet::getType, params.get("type"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("amountType")), SellerWallet::getAmountType, params.get("amountType"));
        wrapper.lambda().like(ObjectUtil.isNotEmpty(params.get("remark")), SellerWallet::getRemark, params.get("remark"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("sourceId")), SellerWallet::getSourceId, params.get("sourceId"));
		wrapper.lambda().in(ObjectUtil.isNotEmpty(params.get("sourceType")), SellerWallet::getSourceType, Func.toStr(String.valueOf(params.get("sourceType"))));
		wrapper.lambda().ge(ObjectUtil.isNotEmpty(params.get("createTimeBegin")), SellerWallet::getCreateTime, params.get("createTimeBegin"));
		wrapper.lambda().le(ObjectUtil.isNotEmpty(params.get("createTimeEnd")), SellerWallet::getCreateTime, params.get("createTimeEnd"));
        wrapper.lambda().orderByDesc(SellerWallet::getId);
        return wrapper;
    }

	/**
	 * 统计金额
	 */
	public Double sumAmountByParams(Map<String, Object> params) {
		QueryWrapper<SellerWallet> wrapper = selectSellerWalletWrapper(params);
		wrapper.select("ifnull(sum(amount),0) as total");
		return Double.valueOf(getMap(wrapper).get("total").toString());
	}

}

